Name "SmartKiwi Windows"
OutFile "SmartKiwi-Windows.exe"
# define uninstaller name

InstallDir "$PROGRAMFILES\SmartKiwiWindows"

!include "EnvVarUpdate.nsh"
!include "MUI.nsh"


!define MUI_ICON "Logo-icone.ico"
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP_NOSTRETCH
!define MUI_HEADERIMAGE_BITMAP "logo.bmp"

Var JavaInstallationPath
Var ChromeInstallationPath
Page custom CheckInstalledJRE
!insertmacro MUI_LANGUAGE "French" 
!define MUI_WELCOMEPAGE_TITLE "welcome_title"

  !define MUI_WELCOMEPAGE_TEXT "welcome_text"
  !insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
DirText "Veuillez selectionner le repertoire d'installation de SmartKiwi Windows"
Section
Messagebox MB_OK "$INSTDIR"
# define the output path for this file
SetOutPath $INSTDIR
Messagebox MB_OK "$INSTDIR"
# define what to install and place it in the output path
File Smart-kiwi-sender.jar
File Smart-kiwi-service.jar
File config.properties
File Logo-icone.ico
File smartkiwiico.gif
File smartkiwiico.ico
File smartkiwilogo.gif
File SmartKiwi-Chrome.crx
Messagebox MB_OK "$INSTDIR"
${EnvVarUpdate} $0 "SMART_KIWI_WINDOWS" "A" "HKLM" $INSTDIR
Messagebox MB_OK "$INSTDIR"
WriteRegStr HKCR "*\shell\Envoyer vers Smart Kiwi\command" "" "$\"$JavaInstallationPath\bin\javaw.exe$\" -jar $\"$INSTDIR\Smart-kiwi-sender.jar$\" $\"%1$\""
WriteRegExpandStr HKCR "*\shell\Envoyer vers Smart Kiwi" "Icon" "$INSTDIR\smartkiwiico.ico"
WriteRegStr HKCR "*\shell\Envoyer vers Smart Kiwi en indiquant une fiche\command" "" "$\"$JavaInstallationPath\bin\javaw.exe$\" -jar $\"$INSTDIR\Smart-kiwi-sender.jar$\" $\"%1$\" true"
WriteRegExpandStr HKCR "*\shell\Envoyer vers Smart Kiwi en indiquant une fiche" "Icon" "$INSTDIR\smartkiwiico.ico"
WriteUninstaller $INSTDIR\Smart-Kiwi-Uninstaller.exe
SectionEnd

Section "Chrome plugin"
	Call CheckInstallerChrome
	
	StrCmp $2 "" NoChrome InstallPlugin
	
	InstallPlugin:
		WriteRegStr HKLM "Software\Google\Chrome\Extensions\Smartkiwichrome30032015" "path" "${INSTALL_DIR}\SmartKiwi-Chrome.crx"
		WriteRegStr HKLM "Software\Google\Chrome\Extensions\Smartkiwichrome30032015" "version"     "1.0.0"
	
	NoChrome:
		
SectionEnd

Section "Uninstall"
  Delete $INSTDIR\Smart-Kiwi-Uninstaller.exe ; delete self (see explanation below why this works)
  Delete $INSTDIR\Smart-kiwi-sender.jar
  Delete $INSTDIR\Smart-kiwi-service.jar
  Delete $INSTDIR\config.properties
  Delete $INSTDIR\smartkiwiico.gif
  Delete $INSTDIR\smartkiwiico.ico
  Delete $INSTDIR\Logo-icone.ico
  Delete $INSTDIR\smartkiwilogo.gif
  Delete $INSTDIR\SmartKiwi-Chrome.crx
  RMDir $INSTDIR
  ${un.EnvVarUpdate} $0 "SMART_KIWI_WINDOWS" "R" "HKLM" $INSTDIR
  DeleteRegKey HKCR "*\shell\Envoyer vers Smart Kiwi"
  DeleteRegKey HKCR "*\shell\Envoyer vers Smart Kiwi en indiquant une fiche"
  DeleteRegKey HKLM "Software\Google\Chrome\Extensions\Smartkiwichrome30032015"
SectionEnd

Function CheckInstallerChrome
	DetectTry1:
	ReadRegStr $2 HKLM "ChromeHTML\shell\open\command" ""
	StrCmp $2 "" NoChrome GetValue
	GetValue:
	StrCpy $ChromeInstallationPath $2
	Goto Done
	NoChrome:
	Goto Done
	Done:
	Messagebox MB_OK "$2" 
FunctionEnd

Function CheckInstalledJRE
    DetectTry1:
    StrCpy $1 "SOFTWARE\JavaSoft\Java Runtime Environment"  
    StrCpy $2 0  
    ReadRegStr $2 HKLM "$1" "CurrentVersion"  
    StrCmp $2 "" DetectTry2 JRE 
    JRE:
    ReadRegStr $5 HKLM "$1\$2" "JavaHome"  
    StrCmp $5 "" DetectTry2 GetValue    

    DetectTry2:  
    ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Development Kit" "CurrentVersion"  
    StrCmp $2 "" NoJava JDK 
    JDK:
    ReadRegStr $5 HKLM "SOFTWARE\JavaSoft\Java Development Kit\$2" "JavaHome"  
    StrCmp $5 "" NoJava GetValue

    GetValue:
    StrCpy $JavaInstallationPath $5 
    Goto done

    NoJava:  
    Messagebox MB_OK "No Java installation detected. Please install."  
	Quit
    done:   
    #$JavaInstallationPath should contain the system path to Java
FunctionEnd